package 每日一题;

import java.util.*;

/**
 * @author 会玩的洋洋
 * https://leetcode-cn.com/problems/sum-of-unique-elements/
 */
public class _1748_唯一元素的和 {
    /**
     * 普通哈希表
     * @param nums 传入数组
     * @return int
     */
    public int sumOfUniqueByHash(int[] nums) {
        int ans = 0;
        Map<Integer, Boolean> hash = new HashMap<>();
        for (int num : nums) {
            if (hash.containsKey(num)) {
                hash.put(num, false);
            } else {
                hash.put(num, true);
            }
        }
        for (Map.Entry<Integer, Boolean> entry : hash.entrySet()) {
            if (entry.getValue()) {
                ans += entry.getKey();
            }
        }
        return ans;
    }

    /**
     * 排序 + 双指针
     * @param nums 传入数组
     * @return int
     */
    public int sumOfUniqueBySort(int[] nums) {
        int ans = 0;
        Arrays.sort(nums);
        int n = nums.length;
        for (int i = 0; i < n; i++) {
            int j = i;
            while (j < n && nums[i] == nums[j]) {
                ++j;
            }
            if (j - i == 1) {
                ans += nums[i];
            }
            i = j;
        }
        return ans;
    }

    int[] cnt = new int[100];
    public int sumOfUnique(int[] nums) {
        int ans = 0;
        for (int num : nums) {
            ++cnt[num];
        }
        for (int i = 0; i <= 100; i++) {
            if (cnt[i] == 1) {
                ans += i;
            }
        }
        return ans;
    }
}
